package com.zahb.qadx.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zahb.qadx.R;
import com.zahb.qadx.model.Mission24;
import com.zahb.qadx.util.ImageLoaderKt;
import com.zhpan.bannerview.BaseBannerAdapter;
import com.zhpan.bannerview.BaseViewHolder;

public class ByLanguageAdapter extends BaseBannerAdapter<Mission24.SafetySloganListBean, ByLanguageAdapter.BannerViewHolder> {

     public static Context mContexts;
     public static  Mission24 mission24;

    @Override
    protected void onBind(ByLanguageAdapter.BannerViewHolder holder, Mission24.SafetySloganListBean data, int position, int pageSize) {
        holder.bindData(data, position, pageSize);
    }
  
    public ByLanguageAdapter(Context mContext ,Mission24 mission24) {
        this.mContexts = mContext;
        this.mission24=mission24;

    }
    @Override
    public ByLanguageAdapter.BannerViewHolder createViewHolder(@NonNull ViewGroup parent, View itemView, int viewType) {
        return new ByLanguageAdapter.BannerViewHolder(itemView);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_bylanguage_view;
    }

    public static class BannerViewHolder extends BaseViewHolder<Mission24.SafetySloganListBean> {
        boolean isCompletedDraw = false;
        BannerViewHolder(@NonNull View itemView) {
            super(itemView);

        }

        @Override
        public void bindData(Mission24.SafetySloganListBean data, int position, int pageSize) {

            TextView tvRead = findView(R.id.tvRead);//侧量宽度
            ImageView imageView = findView(R.id.iv_banner);
            TextView mTextView = findView(R.id.enterprise_slogan);
            TextView celebrity = findView(R.id.celebrity);
            tvRead.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (!isCompletedDraw){
                        isCompletedDraw = true;
                        // h就是TextView的高度,h就是TextView的宽度
                        int h = tvRead.getMeasuredHeight();
                        int w = tvRead.getMeasuredWidth();
                        //为ImageView设置宽高
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mTextView.getLayoutParams();
                        params.width = w;
                        mTextView.setLayoutParams(params);
                    }
                }
            });

            mTextView.setText(data.getSlogan());
            celebrity.setText(data.getSign());
            ImageLoaderKt.loadImageSameCorners(imageView, mission24.getBgImg(), 20f);

//            imageView.setOnClickListener(new View.OnClickListener() {
//
//                @Override
//                public void onClick(View view) {
////                    myDialog.show();
////                    Toast.makeText(mContexts, ""+position, Toast.LENGTH_SHORT).show();
//
//                }
//            });


        }

    }
}
